/**
 * Copyright (c) 2006-2017, JGraph Ltd
 * Copyright (c) 2006-2017, Gaudenz Alder
 */
NextcloudClient = function(editorUi)
{
    DrawioClient.call(this, editorUi, 'dbauth');
};

// Extends DrawioClient
mxUtils.extend(NextcloudClient, DrawioClient);

/**
 * FIXME: How to find name of app folder for current user. The Apps part of the
 * name is internationalized so this hardcoded check does not work everywhere.
 */
NextcloudClient.prototype.appPath = '/drawio/';

/**
 * Executes the first step for connecting to Google Drive.
 */
NextcloudClient.prototype.extension = '.cxl';

/**
 * Executes the first step for connecting to Google Drive.
 */
NextcloudClient.prototype.writingFile = false;

/**
 * Executes the first step for connecting to Google Drive.
 */
NextcloudClient.prototype.maxRetries = 4;

/**
 * Authorizes the client, gets the userId and calls <open>.
 */
NextcloudClient.prototype.logout = function()
{

};


/**
 * Authorizes the client, gets the userId and calls <open>.
 */
NextcloudClient.prototype.authenticate = function(success, error)
{
};

/**
 * Authorizes the client, gets the userId and calls <open>.
 */
NextcloudClient.prototype.executePromise = function(promise, success, error)
{
};


NextcloudClient.prototype.getLibrary = function(path, success, error)
{
    error('未实现!');
};


NextcloudClient.prototype.getFile = function(path, success, error, asLibrary)
{
    error('未实现!');
};


NextcloudClient.prototype.readFile = function(arg, success, error, binary)
{
    error('未实现!');
};

NextcloudClient.prototype.checkExists = function(filename, fn, noConfirm)
{
    error('未实现!');
};

NextcloudClient.prototype.pickFile = function()
{
    let dlg = new NextcloudDialog(this.ui,this);
    this.ui.spinner.spin(document.body, 'loading...')
    window.sendRequest('get',FILE_NEXTCLOUD_URL,null,null,null,
        (data)=>{
            this.ui.spinner.stop()
            this.ui.showDialog(dlg.container, 420, 360, true, true);
            dlg.init(data)
        },(xhr)=>{
            this.ui.spinner.stop();
            this.ui.handleError(xhr.responseText);
        },true)
};


NextcloudClient.prototype.insertFile = function(filename, format, data, success, error,replace)
{
    window.sendRequest('post',FILE_NEXTCLOUD_URL,{rewrite:replace},{name: filename + '.' + format, format,data,rewrite:replace},null,
        (data)=>{
            if (success != null) {
                success();
            }
        },(xhr)=>{
            if (error != null) {
                error(xhr.responseText);
            }
        },true)
};


NextcloudClient.prototype.saveFile = function(filename, data, success, error, folder)
{
    error('未实现!');
};


NextcloudClient.prototype.createFile = function(file, success, error)
{
    error('未实现!');
};
